package com.yonyou.sto.modules.ims.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yonyou.sto.common.api.CommonPage;
import com.yonyou.sto.common.api.CommonResult;
import com.yonyou.sto.common.domain.DownloadFile;
import com.yonyou.sto.modules.ims.dto.ImsPartySelectParam;
import com.yonyou.sto.modules.ims.model.ImsParty;
import com.yonyou.sto.modules.ims.service.ImsPartyService;
import com.yonyou.sto.modules.ums.model.UmsAdmin;
import com.yonyou.sto.modules.ums.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aegon
 * @since 2020-09-15
 */
@RestController
@RequestMapping("/server/ims/imsParty")
@Api(tags = "ImsPartyController", description = "组织管理")
public class ImsPartyController {

    @Resource
    private ImsPartyService imsPartyService;

    @Resource
    private UmsAdminService adminService;

    @ApiOperation(value = "新增组织")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult create(@Validated @RequestBody ImsParty imsParty) {
        return imsPartyService.create(imsParty);
    }

    @ApiOperation(value = "修改组织")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long id, @RequestBody ImsParty imsParty) {
        if (StrUtil.isNotBlank(imsParty.getVerifyUsername())) {
            UmsAdmin umsAdmin = adminService.getAdminByUsername(imsParty.getVerifyUsername());
            imsParty.setVerifyNickname(umsAdmin.getNickName());
        }
        imsParty.setId(id);
        boolean success = imsPartyService.updateById(imsParty);
        if (success) {
            return CommonResult.success("success");
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "删除组织")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestParam Long id) {
        boolean success = imsPartyService.delete(id);
        if (success) {
            return CommonResult.success("success");
        }
        return CommonResult.failed("有此组织的收款单,不能删除");
    }

    @ApiOperation(value = "根据组织编码名称分页查询组织")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<ImsParty>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<ImsParty> partyPage = imsPartyService.list(keyword, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(partyPage));
    }

    @ApiOperation(value = "获取组织选择框数据")
    @GetMapping(value = "/getPartySelect")
    public CommonResult<List<ImsPartySelectParam>> getPartySelect() {
        return CommonResult.success(imsPartyService.getPartySelect());
    }

    @ApiOperation(value = "下载导入模板")
    @GetMapping(value = "/downloadFile")
    public void downloadFile(HttpServletRequest request, HttpServletResponse response){
        try {
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("partyTemplate.xlsx");
            byte[] fileData = DownloadFile.input2byte(is);
            DownloadFile.downloadFile(response, request, "partyTemplate.xlsx", fileData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

    @ApiOperation(value = "组织导入excel")
    @PostMapping(value = "/importParty")
    public CommonResult importParty(MultipartFile file) {
        return imsPartyService.importParty(file);
    }


}

